
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言范围(Range) · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part17/" />
    
    
    <link rel="prev" href="../part15/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.17" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言范围(Range)</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x8303;&#x56F4;range">Go &#x8BED;&#x8A00;&#x8303;&#x56F4;(Range)</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x4E2D; range &#x5173;&#x952E;&#x5B57;&#x7528;&#x4E8E; for &#x5FAA;&#x73AF;&#x4E2D;&#x8FED;&#x4EE3;&#x6570;&#x7EC4;(array)&#x3001;&#x5207;&#x7247;(slice)&#x3001;&#x901A;&#x9053;(channel)&#x6216;&#x96C6;&#x5408;(map)&#x7684;&#x5143;&#x7D20;&#x3002;&#x5728;&#x6570;&#x7EC4;&#x548C;&#x5207;&#x7247;&#x4E2D;&#x5B83;&#x8FD4;&#x56DE;&#x5143;&#x7D20;&#x7684;&#x7D22;&#x5F15;&#x548C;&#x7D22;&#x5F15;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#xFF0C;&#x5728;&#x96C6;&#x5408;&#x4E2D;&#x8FD4;&#x56DE; key-value &#x5BF9;&#x3002;</p>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">//&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x4F7F;&#x7528;range&#x53BB;&#x6C42;&#x4E00;&#x4E2A;slice&#x7684;&#x548C;&#x3002;&#x4F7F;&#x7528;&#x6570;&#x7EC4;&#x8DDF;&#x8FD9;&#x4E2A;&#x5F88;&#x7C7B;&#x4F3C;</span>
    nums := [] <span class="hljs-keyword">int</span> {<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>}
    sum := <span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> _, num := <span class="hljs-keyword">range</span> nums {
        sum += num
    }
    fmt.Println(<span class="hljs-string">&quot;sum:&quot;</span>, sum)
    <span class="hljs-comment">//&#x5728;&#x6570;&#x7EC4;&#x4E0A;&#x4F7F;&#x7528;range&#x5C06;&#x4F20;&#x5165;index&#x548C;&#x503C;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x3002;&#x4E0A;&#x9762;&#x90A3;&#x4E2A;&#x4F8B;&#x5B50;&#x6211;&#x4EEC;&#x4E0D;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x8BE5;&#x5143;&#x7D20;&#x7684;&#x5E8F;&#x53F7;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7A7A;&#x767D;&#x7B26;&quot;_&quot;&#x7701;&#x7565;&#x4E86;&#x3002;&#x6709;&#x65F6;&#x4FAF;&#x6211;&#x4EEC;&#x786E;&#x5B9E;&#x9700;&#x8981;&#x77E5;&#x9053;&#x5B83;&#x7684;&#x7D22;&#x5F15;&#x3002;</span>
    <span class="hljs-keyword">for</span> i, num := <span class="hljs-keyword">range</span> nums {
        <span class="hljs-keyword">if</span> num == <span class="hljs-number">3</span> {
            fmt.Println(<span class="hljs-string">&quot;index:&quot;</span>, i)
        }
    }
    <span class="hljs-comment">// range&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x5728;map&#x7684;&#x952E;&#x503C;&#x5BF9;&#x4E0A;&#x3002;</span>
    kvs := <span class="hljs-keyword">map</span> [<span class="hljs-keyword">string</span>] <span class="hljs-keyword">string</span> {<span class="hljs-string">&quot;a&quot;</span>: <span class="hljs-string">&quot;apple&quot;</span>, <span class="hljs-string">&quot;b&quot;</span>: <span class="hljs-string">&quot;banana&quot;</span>}
    <span class="hljs-keyword">for</span> k, v := <span class="hljs-keyword">range</span> kvs {
        fmt.Printf(<span class="hljs-string">&quot;%s -&gt; %s\n&quot;</span>, k, v)
    }
    <span class="hljs-comment">// range&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x679A;&#x4E3E;Unicode&#x5B57;&#x7B26;&#x4E32;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x7B26;&#x7684;&#x7D22;&#x5F15;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x662F;&#x5B57;&#x7B26;&#xFF08;Unicode&#x7684;&#x503C;&#xFF09;&#x672C;&#x8EAB;&#x3002;</span>
    <span class="hljs-keyword">for</span> i, c := <span class="hljs-keyword">range</span> <span class="hljs-string">&quot;123&quot;</span> {
        fmt.Println(i, c)
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">sum: 9
index: 1
a -&gt; apple
b -&gt; banana
0 49
1 50
2 51
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part15/" class="navigation navigation-prev " aria-label="Previous page: Go 语言切片(Slice)">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part17/" class="navigation navigation-next " aria-label="Next page: Go 语言Map(集合)">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言范围(Range)","level":"1.17","depth":1,"next":{"title":"Go 语言Map(集合)","level":"1.18","depth":1,"path":"part17/README.md","ref":"part17/README.md","articles":[]},"previous":{"title":"Go 语言切片(Slice)","level":"1.16","depth":1,"path":"part15/README.md","ref":"part15/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part16/README.md","mtime":"2021-01-20T06:45:02.917Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

